package com.qf.index;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUnit {

    /**
     * 这是一套检查的程序，用于检查规范的中国医院和世界上的大学的名称是否正确，以及是否规范，如果没有规范，会保存到checkMultipleTable中（分大学和医院），人工添加规范后，再保存进infoAndNameMap中（分大学和医院，保存进字典文件），下次即可以使用了。
     * 贾新志 2025.02.10
     */
    public static void main(String[] args) throws SQLException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date()) + " 程序开始了~~~");
        String newDataName = "scholar_25_01.";
        String pmUtAidInfoTypeTable = newDataName + "pm_ut_aid_info_0120_type";
        String chinaHospNameTable = newDataName + "tb_true_china_hosp_all_final";
        String checkMultipleHospTable = newDataName + "multiple_hosp_check";

        String unitSumOrdinaryTable = newDataName + "unit_sum_ordinary_0701";
        String unitSumOrdinaryHospTable = newDataName + "unit_sum_ordinary_hosp_finish";
        String checkMultipleUnivTable = newDataName + "multiple_univ_check";
        String checkMultipleHospTableTwo = newDataName + "multiple_hosp_check_two";
        String checkMultipleUnivTableTwo = newDataName + "multiple_univ_check_two";

        // 第一步，将历史上办公室和王承伟规范的单位名称写入字典文件，字典文件共4个，分别是医院和大学，以及标准的医院和大学。
//        saveInfoAndNameUniv(unitSumOrdinaryTable, "id", "info`,`", "F:\\WorkSpaces\\Data\\src\\main\\resources\\dict\\rule_univ_name.txt", "univ");
//        saveInfoAndNameUniv(unitSumOrdinaryTable, "id", "info`,`", "F:\\WorkSpaces\\Data\\src\\main\\resources\\dict\\rule_univ_name.txt", "hosp");
//        saveInfoAndNameUniv(unitSumOrdinaryHospTable, "id", "info`,`", "F:\\WorkSpaces\\Data\\src\\main\\resources\\dict\\rule_univ_name.txt", "hosp");
//        saveInfoAndNameHosp(chinaHospNameTable, "id", "info`,`final", "F:\\WorkSpaces\\Data\\src\\main\\resources\\dict\\rule_hosp_name.txt");
//        Map<String, String> infoAndNameMap = getInfoAndNameMap();
//        // 根据字典文件进行检查，分别为医院和大学，后续有小葛写一个页面，有办公室人员人工再次核实后，保存进字典文件，再次跑程序，可以反复多次操作后作为最终结果使用。类型分4种，1:中国医院，2:指南，3:pubmed，4:自2019年以来
//        check(pmUtAidInfoTypeTable, "id", "type`,`unit", checkMultipleHospTable, infoAndNameMap, "1");
//        System.out.println(format.format(new Date()) + " 医院表结束了~~~");
//        check(pmUtAidInfoTypeTable, "id", "type`,`unit", checkMultipleUnivTable, infoAndNameMap, "4");
//
//        // 将大学和医院两个部分的，分成数量接近
//        twoTable(checkMultipleHospTable, "id", "sum`,`size`,`hosp`,`info`,`h1`,`h2`,`example", checkMultipleUnivTable, checkMultipleHospTableTwo, checkMultipleUnivTableTwo);

        // 判断是否需要核实
        isCheck("id", checkMultipleHospTableTwo, checkMultipleUnivTableTwo);
        System.out.println(format.format(new Date()) + " 程序全部结束了。");
    }


    public static void check(String pmUtAidInfoTypeTable, String autoId, String field, String checkMultipleTable, Map<String, String> infoAndNameMap, String type) throws SQLException {
        int onePageNum = 1000;
        Map<String, Integer> unitAndSumMap = new HashMap<>();
        Map<String, Set<Integer>> unitAndAidSetMap = new HashMap<>();
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + pmUtAidInfoTypeTable).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + pmUtAidInfoTypeTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    if (type.equals(entry.getStr("type"))) {
                        String units = entry.getStr("unit");
                        if (null != units && units.contains("\"}]")) {
                            List<En.FourItems> unitList = JSONObject.parseArray(units, En.FourItems.class);
                            for (En.FourItems fourItems : unitList) {
                                List<Integer> aidList = fourItems.getAidList();
                                int size = fourItems.getAidList().size();
                                String unit = fourItems.getName().toLowerCase();
                                unitAndSumMap.compute(unit, (key, value) -> value != null ? (value + size) : size);
                                if (unitAndAidSetMap.containsKey(unit)) {
                                    Set<Integer> aidSet = unitAndAidSetMap.get(unit);
                                    if (aidSet.size() < 10) {
                                        for (Integer aid : aidList) {
                                            aidSet.add(aid);
                                            if (aidSet.size() == 10) {
                                                unitAndAidSetMap.put(unit, aidSet);
                                                break;
                                            }
                                        }
                                    }
                                } else {
                                    Set<Integer> aidSet = new HashSet<>();
                                    for (Integer aid : aidList) {
                                        aidSet.add(aid);
                                        if (aidSet.size() == 10) {
                                            unitAndAidSetMap.put(unit, aidSet);
                                            break;
                                        }
                                    }
                                    if (aidSet.size() != 10) {
                                        unitAndAidSetMap.put(unit, aidSet);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Db.use().execute("truncate table " + checkMultipleTable);
        unitAndSumMap.keySet().parallelStream().forEach(info -> {
            String hosp = "";
            List<String> infoList = new ArrayList<>(Arrays.asList(info.split("; ")));
            Set<String> resultSet = new HashSet<>();
            Map<String, Integer> resultAndSumMap = new HashMap<>();
            if (infoAndNameMap.containsKey(info)) {
                hosp = infoAndNameMap.get(info);
            } else if (info.contains("; ")) {
                String unit = listToUnit(dellRepeatList(infoList));
                if (infoAndNameMap.containsKey(unit)) {
                    hosp = infoAndNameMap.get(unit);
                }
            } else {
                for (Map.Entry<String, String> entry : infoAndNameMap.entrySet()) {
                    String[] mapSplit = entry.getKey().split("; ");
                    Set<String> set = new HashSet<>(Arrays.asList(mapSplit));
                    boolean bool = true;
                    for (String s : infoList) {
                        if (!set.contains(s)) {
                            bool = false;
                            break;
                        }
                    }
                    if (bool) {
                        String name = entry.getValue().contains("_") ? entry.getValue() : "中国_" + entry.getValue();
                        resultSet.add(entry.getKey() + "=" + name);
                        if ((entry.getKey().contains("hospital") && name.contains("医院")) || (entry.getKey().contains("university") && name.contains("大学"))) {
                            resultAndSumMap.compute(name, (key, value) -> value != null ? (value + 1) : 1);
                        }
                    }
                }
            }
            String h2 = "";
            int max = 0;
            for (Map.Entry<String, Integer> entry : resultAndSumMap.entrySet()) {
                if (entry.getValue() > max) {
                    max = entry.getValue();
                    h2 = entry.getKey();
                }
            }
            Set<Integer> exampleSet = unitAndAidSetMap.containsKey(info) ? unitAndAidSetMap.get(info) : new HashSet<>();
            try {
                Db.use().insert(Entity.create(checkMultipleTable).set("info", info).set("h2", h2).set("h1", JSONUtil.toJsonStr(resultSet)).set("example", JSONUtil.toJsonStr(exampleSet)).set("hosp", hosp).set("sum", unitAndSumMap.get(info)));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
    }

    public static String listToUnit(List<String> unitList) {
        if (null == unitList || unitList.isEmpty()) {
            return "";
        }
        // 为了保证信息的一致性，本方法的代码不能修改。
        Collections.sort(unitList);
        String unit = CollUtil.join(unitList, "; ").toLowerCase().trim();
        String l = unitList.get(0).toLowerCase().trim();
        boolean isHosp = l.contains("hosp ") || l.contains(" hosp") || l.contains("hospital") || l.contains("医院");
        unit = unitList.size() == 2 && isHosp ? (unitList.get(1) + "; " + l).toLowerCase().trim() : unit;
        unit = unitList.size() == 3 && isHosp ? (unitList.get(1) + "; " + unitList.get(2) + "; " + l).toLowerCase().trim() : unit;
        return unit;
    }

    public static List<String> dellRepeatList(List<String> list) {
        // 如果list内存在某元素包含另一元素，则将被包含的给予删除。[Kaohsiung Medical University, Kaohsiung Medical University Hospital] -> [Kaohsiung Medical University Hospital]
        List<String> result = new ArrayList<>(list);
        for (String s : list) {
            for (String t : list) {
                if (!s.equalsIgnoreCase(t) && s.toLowerCase().contains(t.toLowerCase())) {
                    result.remove(t);
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    public static void saveInfoAndNameHosp(String chinaHospNameTable, String autoId, String field, String savePath) throws SQLException {
        int onePageNum = 1000;
        Set<String> set = new HashSet<>();
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + chinaHospNameTable).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + chinaHospNameTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    String info = entry.getStr("info");
                    String hosp = entry.getStr("final");
                    if (null != info && null != hosp && info.contains("\"]") && hosp.length() > 3) {
                        String unit = listToUnit(JSONObject.parseArray(info, String.class));
                        String name = unit + "Łł" + "中国_" + hosp;
                        if (!set.contains(name) && unit.length() > 3) {
                            FileUtil.appendUtf8String(name + System.lineSeparator(), savePath);
                            set.add(name);
                        }
                    }
                }
            }
        }
    }

    public static void saveInfoAndNameUniv(String unitSumOrdinaryTable, String autoId, String field, String savePath, String univOrHosp) throws SQLException {
        int onePageNum = 1000;
        Set<String> set = new HashSet<>();
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + unitSumOrdinaryTable).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + univOrHosp + "` from " + unitSumOrdinaryTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    String info = entry.getStr("info");
                    String univs = entry.getStr(univOrHosp);
                    if (null != info && null != univs && info.contains("\"]") && univs.contains("coun")) {
                        String unit = listToUnit(JSONObject.parseArray(info, String.class));
                        List<SearchSaveInfo> univList = JSONObject.parseArray(univs, SearchSaveInfo.class);
                        for (SearchSaveInfo searchSaveInfo : univList) {
                            String coun = searchSaveInfo.getCoun().trim();
                            String cn = searchSaveInfo.getCn().trim();
                            if (cn.length() > 3 && coun.length() > 1 && unit.length() > 3) {
                                String name = unit + "Łł" + coun + "_" + cn;
                                if (!set.contains(name)) {
                                    FileUtil.appendUtf8String(name + System.lineSeparator(), savePath);
                                    set.add(name);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static Map<String, String> getInfoAndNameMap() {
        // 注意，机构规范是有顺序的，质量最高的顺序依次是：rule_hosp_name_right->rule_univ_name_right->rule_hosp_name->rule_univ_name，这里采用的Map是会自动覆盖的。
        Map<String, String> infoAndNameMap = new HashMap<>();
        putInfoAndNameMap("dict/rule_univ_name.txt", infoAndNameMap);
        putInfoAndNameMap("dict/rule_hosp_name.txt", infoAndNameMap);
        putInfoAndNameMap("dict/rule_univ_name_right.txt", infoAndNameMap);
        putInfoAndNameMap("dict/rule_hosp_name_right.txt", infoAndNameMap);
        System.out.println("infoAndNameMap.size(): " + infoAndNameMap.size());
        return infoAndNameMap;
    }

    public static void putInfoAndNameMap(String filePath, Map<String, String> infoAndNameMap) {
        List<String> list = FileUtil.readLines(filePath, "utf-8");
        for (String s : list) {
            String[] split = s.split("Łł");
            if (split.length == 2 && s.contains("_")) {
                infoAndNameMap.put(split[0], split[1]);
            }
        }
    }

    @Data
    public static class SearchSaveInfo implements Serializable {
        private static final long serialVersionUID = 1L;
        private int unitSumOrdinaryId = 0; // 表的自增主键，这里用来作为机构的唯一识别码，多个之间用|分隔
        private String coun = "";
        private String cn = "";
        private String en = "";
        private String abbr = "";
        private String old = "";
        private String url = "";   //单位官网url
        private String alias = ""; //单位别名 多个以|隔开
        private String exclude = ""; // 排除，可以多个以"|"隔开
        private String contain = ""; // 包含，可以多个以"|"隔开
        private List<List<String>> mustListList = new ArrayList<>(); // 这里是检索词的包含
        private List<String> excludeList = new ArrayList<>(); // 这里是检索词的排除
    }

    @Data
    public static class InfoAndName implements Serializable {
        private static final long serialVersionUID = 1L;
        private int sum = 0;
        private int size = 0;
        private String hosp = "";
        private String info = "";
        private String h1 = "";
        private String h2 = "";
        private String example = "";
    }

    public static Map<String, InfoAndName> putInfoAndNameMap(String checkMultipleHospTable, String autoId, String field) throws SQLException {
        int onePageNum = 1000;
        Map<String, InfoAndName> infoAndNameMap = new HashMap<>();
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + checkMultipleHospTable).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + checkMultipleHospTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    String info = entry.getStr("info");
                    if (null != info && info.length() > 2) {
                        // 为什么使用infoAndNameMap，而不是直接存表，原因是医院表中的数量（size）有可能小于大学表中的数量，为了使其统一，所以先存到infoAndNameMap，读大学表时再覆盖，这样就保证了均使用大学表中的数量。
                        infoAndNameMap.put(info, getInfoAndName(entry));
                    }
                }
            }
        }
        return infoAndNameMap;
    }

    public static InfoAndName getInfoAndName(Entity entry) {
        InfoAndName infoAndName = new InfoAndName();
        infoAndName.setSum(Utils.getIntField(entry, "sum"));
        infoAndName.setSize(Utils.getIntField(entry, "size"));
        infoAndName.setHosp(Utils.getStrField(entry, "hosp"));
        infoAndName.setInfo(Utils.getStrField(entry, "info"));
        infoAndName.setH1(Utils.getStrField(entry, "h1"));
        infoAndName.setH2(Utils.getStrField(entry, "h2"));
        infoAndName.setExample(Utils.getStrField(entry, "example"));
        return infoAndName;
    }

    public static void twoTable(String checkMultipleHospTable, String autoId, String field, String checkMultipleUnivTable, String checkMultipleHospTableTwo, String checkMultipleUnivTableTwo) throws SQLException {
        // 原来的医院和大学，分为两张表，一张是中国医院以及所有中文，一张是除此之外的其它，分两个人来规范，规范后其结果可以保存到字典rule_hosp_name_right和rule_univ_name_right中，再运行一次程序。
        Map<String, InfoAndName> infoAndNameMap = putInfoAndNameMap(checkMultipleHospTable, autoId, field);
        int onePageNum = 1000;
        Db.use().execute("truncate table " + checkMultipleHospTableTwo);
        Db.use().execute("truncate table " + checkMultipleUnivTableTwo);
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + checkMultipleUnivTable).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + checkMultipleUnivTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    String info = entry.getStr("info");
                    if (null != info && info.length() > 2) {
                        InfoAndName infoAndName = getInfoAndName(entry);
                        if (infoAndNameMap.containsKey(info) || Utils.isContainChinese(info) || info.contains("hospital") || info.contains("cancer center") || info.contains("medical center") || info.contains("medical college")
                                || info.contains("hong kong") || info.contains("medical school") || info.contains("medical science") || info.contains("school medical") || info.contains("taiwan") || info.contains("medical university") || info.contains("university medical")) {
                            // 如果infoAndNameMap中已存在，或者是中文的单位信息，给予覆盖或插入到infoAndNameMap中，最后作为医院的来处理。
                            infoAndNameMap.put(info, getInfoAndName(entry));
                        } else {
                            insertTable(infoAndName, checkMultipleUnivTableTwo);
                        }
                    }
                }
            }
        }
        infoAndNameMap.keySet().parallelStream().forEach(info -> {
            insertTable(infoAndNameMap.get(info), checkMultipleHospTableTwo);
        });
    }

    public static void insertTable(InfoAndName infoAndName, String tableName) {
        try {
            Db.use().insert(Entity.create(tableName)
                    .set("sum", infoAndName.getSum())
                    .set("size", infoAndName.getSize())
                    .set("hosp", infoAndName.getHosp())
                    .set("info", infoAndName.getInfo())
                    .set("h1", infoAndName.getH1())
                    .set("h2", infoAndName.getH2())
                    .set("example", infoAndName.getExample()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void tongYiUnitName(String checkMultipleHospTableTwo, String autoId, String field, String user, int min, int max, int sort) throws SQLException {
        // 通义翻译机构的中文名字，最多翻译5次，如果5次仍失败就不再处理了。翻译后的转为实体类，只有在实体有值的时候才认为是成功，不成功则追加一次。翻译的对象为复旦排行榜和STEM的科室名称。如果匹配不上，允许阿里推荐一个，推荐的加的有方括号。
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int onePageNum = 1000;
        int okSum = 0;
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + checkMultipleHospTableTwo).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + checkMultipleHospTableTwo + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
//                    System.out.println("\033[31;1m" + format.format(new Date()) + " 读表现在开始：" + startId + " 表名：" + checkMultipleHospTableTwo + Utils.printMemoryStr() + "\033[0m");
                    for (Entity entry : tableData) {
                        String info = Utils.getStrField(entry, "info");
                        String hosp = Utils.getStrField(entry, "hosp");
                        String h2 = Utils.getStrField(entry, "h2");
                        String tongYi = Utils.getStrField(entry, "tong_yi");
                        int sum = Utils.getIntField(entry, "sum");
                        int id = Utils.getIntField(entry, autoId);
                        String cn = hosp.isEmpty() ? h2 : hosp;
                        cn = cn.isEmpty() ? "中国_清华大学" : cn;
                        int end = id > 10 ? Integer.parseInt(Integer.toString(id).substring(Integer.toString(id).length() - 2)) : id;
                        if ((tongYi.isEmpty() || "1".equals(tongYi) || "2".equals(tongYi) || "3".equals(tongYi) || "4".equals(tongYi) || "5".equals(tongYi)) && sum >= 20 && cn.length() > 2 && id > 0 && (end >= min && end <= max) && !Utils.isContainChinese(info)) {
                            String question = "英文名称:\"" + info.replace("; ", ", ") + "\"，中文名称:\"" + cn + "\"";
                            okSum++;
                            try {
                                String upData = getUpData(tongYi, question, user);
                                System.out.println(format.format(new Date()) + " tongYi:" + tongYi + " question:" + question + " user:" + user + " upData:" + upData + " id:" + id + " sort:" + sort + " okSum:" + okSum);
                                try {
                                    Db.use().update(Entity.create().set("tong_yi", upData).set("question", question).set("datetime", LocalDateTime.now()), Entity.create(checkMultipleHospTableTwo).set(autoId, id));
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            } catch (Exception e) {
//                                throw new RuntimeException(e);
                            }
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void isCheck(String autoId, String checkMultipleHospTableTwo, String checkMultipleUnivTableTwo) throws SQLException {
        // 通义翻译机构的中文名字，最多翻译5次，如果5次仍失败就不再处理了。翻译后的转为实体类，只有在实体有值的时候才认为是成功，不成功则追加一次。翻译的对象为复旦排行榜和STEM的科室名称。如果匹配不上，允许阿里推荐一个，推荐的加的有方括号。
        Set<String> chinaSet = new HashSet<>();
        getChinaSet(checkMultipleHospTableTwo, autoId, "hosp", chinaSet);
        getChinaSet(checkMultipleUnivTableTwo, autoId, "hosp", chinaSet);
        System.out.println("chinaSet.size(): " + chinaSet.size());
        updateCheckTable(autoId, "info`,`hosp`,`h2`,`tong_yi`,`sum", checkMultipleHospTableTwo, chinaSet);
        updateCheckTable(autoId, "info`,`hosp`,`h2`,`tong_yi`,`sum", checkMultipleUnivTableTwo, chinaSet);
    }

    public static void updateCheckTable(String autoId, String field, String checkMultipleHospTableTwo, Set<String> chinaSet) throws SQLException {
        int onePageNum = 1000;
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + checkMultipleHospTableTwo).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + checkMultipleHospTableTwo + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                    for (Entity entry : tableData) {
                        String info = Utils.getStrField(entry, "info");
                        String hosp = Utils.getStrField(entry, "hosp");
                        String h2 = Utils.getStrField(entry, "h2");
                        String tongYi = Utils.getStrField(entry, "tong_yi");
                        int sum = Utils.getIntField(entry, "sum");
                        int id = Utils.getIntField(entry, autoId);
                        int check = 0;
                        String recommend = "";
                        if (sum >= 20) {
                            List<En.YnRecommend> tongYiList = tongYi.contains("\"}]") ? JSONObject.parseArray(tongYi, En.YnRecommend.class) : new ArrayList<>();
                            En.YnRecommend ynRecommend = null != tongYiList && !tongYiList.isEmpty() ? tongYiList.get(0) : new En.YnRecommend();
                            hosp = hosp.isEmpty() ? h2 : hosp;
                            String china = "中国_" + info;
                            if (Utils.isContainChinese(info)) {
                                if (info.contains("; ")) {
                                    String[] split = info.split("; ");
                                    if (split.length == 2) {
                                        if (split[0].contains("大学") && split[1].contains("医院")) {
                                            china = "中国_" + split[1];
                                        } else if (split[1].contains("大学") && split[0].contains("医院")) {
                                            china = "中国_" + split[0];
                                        }
                                    }
                                }
                            }
                            // 如果医院是空，但来自info中的名称被chinaSet包含，说明这个是规范过的中国的机构，可以直接更新不用核实。
                            if (hosp.isEmpty() && chinaSet.contains(china)) {
                                hosp = china;
                            } else {
                                // 如果通义说不是，需要核实的为通义推荐的，如果通义是空，需要核实的为info（china）
                                if ("不是".equals(ynRecommend.getYn())) {
                                    recommend = ynRecommend.getRecommend();
                                    check = 1;
                                } else if (ynRecommend.getYn().isEmpty()) {
                                    recommend = china;
                                    check = 1;
                                }
                            }
                        }
                        check = hosp.equals(recommend) ? 0 : check;
                        try {
                            Db.use().update(Entity.create().set("hosp", hosp).set("check", check).set("recommend", recommend), Entity.create(checkMultipleHospTableTwo).set(autoId, id));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void getChinaSet(String checkMultipleHospTableTwo, String autoId, String field, Set<String> chinaSet) throws SQLException {
        // 通义翻译机构的中文名字，最多翻译5次，如果5次仍失败就不再处理了。翻译后的转为实体类，只有在实体有值的时候才认为是成功，不成功则追加一次。翻译的对象为复旦排行榜和STEM的科室名称。如果匹配不上，允许阿里推荐一个，推荐的加的有方括号。
        int onePageNum = 1000;
        int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + checkMultipleHospTableTwo).intValue();
        if (tableMax > 0) {
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + checkMultipleHospTableTwo + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                    for (Entity entry : tableData) {
                        String hosp = Utils.getStrField(entry, "hosp");
                        if (hosp.startsWith("中国_") && hosp.length() > 6) {
                            chinaSet.add(hosp);
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getUpData(String tongYi, String question, String user) {
        if (tongYi.isEmpty() || "1".equals(tongYi) || "2".equals(tongYi) || "3".equals(tongYi) || "4".equals(tongYi) || "5".equals(tongYi)) {
            String sendForJson = TongYiApi.sendUnitNameJson(question, user);
            List<En.YnRecommend> unitInfoList = new ArrayList<>();
            En.YnRecommend unitInfo = new En.YnRecommend();
            Matcher ynMatcher = Pattern.compile("\"yn\"\\s*:\\s*\"([^\"]*)\"").matcher(sendForJson);
            Matcher recommendMatcher = Pattern.compile("\"recommend\"\\s*:\\s*\"([^\"]*)\"").matcher(sendForJson);
            if (ynMatcher.find()) {
                unitInfo.setYn(ynMatcher.group(1).trim());
            }
            if (recommendMatcher.find()) {
                unitInfo.setRecommend(recommendMatcher.group(1).trim());
            }
            if (!unitInfo.getYn().isEmpty() || !unitInfo.getRecommend().isEmpty()) {
                unitInfoList.add(unitInfo);
            }
            String upData = "";
            if (unitInfoList.isEmpty()) {
                upData = tongYi.isEmpty() ? "1" : upData;
                upData = "1".equals(tongYi) ? "2" : upData;
                upData = "2".equals(tongYi) ? "3" : upData;
                upData = "3".equals(tongYi) ? "4" : upData;
                upData = "4".equals(tongYi) ? "5" : upData;
                upData = "5".equals(tongYi) ? "6" : upData;
            } else {
                upData = JSONUtil.toJsonStr(unitInfoList);
            }
//            System.out.println("sendForJson: " + sendForJson + " question: " + question + " unitInfoList: " + unitInfoList + " upData: " + upData);
            return upData;
        }
        return tongYi;
    }


    public static Map<Integer, String> getJidAndJournalMap() {
        Map<Integer, String> jidAndJournalMap = new HashMap<>();
        jidAndJournalMap.put(64, "Am J Gastroenterol");
        jidAndJournalMap.put(667, "Gut Microbes");
        jidAndJournalMap.put(786, "Eur J Cancer");
        jidAndJournalMap.put(866, "Eur J Nucl Med Mol Imaging");
        jidAndJournalMap.put(1055, "J Med Chem");
        jidAndJournalMap.put(1304, "Nat Rev Endocrinol");
        jidAndJournalMap.put(1582, "Radiographics");
        jidAndJournalMap.put(1742, "JAMA Intern Med");
        jidAndJournalMap.put(1755, "Eur J Endocrinol");
        jidAndJournalMap.put(1810, "J Cachexia Sarcopenia Muscle");
        jidAndJournalMap.put(1984, "Eur J Vasc Endovasc Surg");
        jidAndJournalMap.put(2471, "Hepatology");
        jidAndJournalMap.put(2888, "Neuropsychopharmacology");
        jidAndJournalMap.put(3155, "Clin Infect Dis");
        jidAndJournalMap.put(3660, "Ultrasound Obstet Gynecol");
        jidAndJournalMap.put(3936, "Lancet Digit Health");
        jidAndJournalMap.put(4428, "Lancet Infect Dis");
        jidAndJournalMap.put(5020, "Trends Parasitol");
        jidAndJournalMap.put(5045, "JACC Cardiovasc Interv");
        jidAndJournalMap.put(5155, "Phytomedicine");
        jidAndJournalMap.put(5537, "Trends Neurosci");
        jidAndJournalMap.put(5568, "JAMA Netw Open");
        jidAndJournalMap.put(5617, "Nat Rev Immunol");
        jidAndJournalMap.put(5697, "Resuscitation");
        jidAndJournalMap.put(5744, "Trends Cancer");
        jidAndJournalMap.put(5762, "Cancer Lett");
        jidAndJournalMap.put(6611, "Ophthalmology");
        jidAndJournalMap.put(7255, "Cardiovasc Diabetol");
        jidAndJournalMap.put(7732, "Nat Neurosci");
        jidAndJournalMap.put(8349, "Trends Pharmacol Sci");
        jidAndJournalMap.put(9209, "Ann Neurol");
        jidAndJournalMap.put(9214, "Periodontol 2000");
        jidAndJournalMap.put(9637, "Circ Heart Fail");
        jidAndJournalMap.put(9675, "EBioMedicine");
        jidAndJournalMap.put(9755, "J Infect");
        jidAndJournalMap.put(9893, "Epilepsia");
        jidAndJournalMap.put(10003, "Mol Neurodegener");
        jidAndJournalMap.put(10376, "Br J Cancer");
        jidAndJournalMap.put(10645, "Thorax");
        jidAndJournalMap.put(10648, "Lancet Respir Med");
        jidAndJournalMap.put(10827, "Addiction");
        jidAndJournalMap.put(12014, "Acta Biomater");
        jidAndJournalMap.put(12192, "Physiol Rev");
        jidAndJournalMap.put(12416, "Cell Mol Immunol");
        jidAndJournalMap.put(12701, "Nat Rev Gastroenterol Hepatol");
        jidAndJournalMap.put(12947, "Neuron");
        jidAndJournalMap.put(13005, "Eur J Heart Fail");
        jidAndJournalMap.put(13339, "JAMA");
        jidAndJournalMap.put(13383, "Nat Cancer");
        jidAndJournalMap.put(13916, "Adv Nutr");
        jidAndJournalMap.put(14329, "J Am Heart Assoc");
        jidAndJournalMap.put(14405, "Stroke");
        jidAndJournalMap.put(14748, "Anaesthesia");
        jidAndJournalMap.put(14835, "Blood Cancer J");
        jidAndJournalMap.put(15356, "Eur Urol Oncol");
        jidAndJournalMap.put(15741, "Lancet Psychiatry");
        jidAndJournalMap.put(15938, "Arthritis Rheumatol");
        jidAndJournalMap.put(16020, "Am J Kidney Dis");
        jidAndJournalMap.put(16957, "Radiology");
        jidAndJournalMap.put(17285, "Br J Surg");
        jidAndJournalMap.put(17604, "Transl Psychiatry");
        jidAndJournalMap.put(17707, "N Engl J Med");
        jidAndJournalMap.put(18275, "Nat Rev Nephrol");
        jidAndJournalMap.put(18480, "J Am Acad Child Adolesc Psychiatry");
        jidAndJournalMap.put(19459, "Drugs");
        jidAndJournalMap.put(19513, "Aliment Pharmacol Ther");
        jidAndJournalMap.put(19518, "Lancet Haematol");
        jidAndJournalMap.put(19777, "Cancer Treat Rev");
        jidAndJournalMap.put(19904, "Thyroid");
        jidAndJournalMap.put(20629, "Mod Pathol");
        jidAndJournalMap.put(21062, "Cell Rep Med");
        jidAndJournalMap.put(21081, "Gastrointest Endosc");
        jidAndJournalMap.put(21280, "Hypertension");
        jidAndJournalMap.put(21646, "Nat Immunol");
        jidAndJournalMap.put(22306, "Int J Nurs Stud");
        jidAndJournalMap.put(22307, "J Neurointerv Surg");
        jidAndJournalMap.put(22548, "JAMA Neurol");
        jidAndJournalMap.put(22617, "Eur Urol");
        jidAndJournalMap.put(23430, "Clin Microbiol Infect");
        jidAndJournalMap.put(23436, "Clin J Am Soc Nephrol");
        jidAndJournalMap.put(23531, "Br J Dermatol");
        jidAndJournalMap.put(23725, "Crit Care");
        jidAndJournalMap.put(23917, "EClinicalMedicine");
        jidAndJournalMap.put(24187, "Am J Ophthalmol");
        jidAndJournalMap.put(24230, "Nat Rev Neurosci");
        jidAndJournalMap.put(24849, "J Heart Lung Transplant");
        jidAndJournalMap.put(25235, "Ann Surg");
        jidAndJournalMap.put(25522, "Sleep Med Rev");
        jidAndJournalMap.put(25569, "Lancet Planet Health");
        jidAndJournalMap.put(25620, "Mol Psychiatry");
        jidAndJournalMap.put(25709, "Lancet Diabetes Endocrinol");
        jidAndJournalMap.put(25851, "Eur Heart J Cardiovasc Imaging");
        jidAndJournalMap.put(25877, "J Dent Res");
        jidAndJournalMap.put(26085, "Cancer Res");
        jidAndJournalMap.put(26848, "Alzheimers Res Ther");
        jidAndJournalMap.put(27546, "Nat Biomed Eng");
        jidAndJournalMap.put(27826, "JAMA Cardiol");
        jidAndJournalMap.put(28444, "Med Image Anal");
        jidAndJournalMap.put(28971, "JACC Cardiovasc Imaging");
        jidAndJournalMap.put(28982, "Mol Cancer");
        jidAndJournalMap.put(29342, "J Neurol Neurosurg Psychiatry");
        jidAndJournalMap.put(29809, "Blood");
        jidAndJournalMap.put(30766, "Am J Clin Nutr");
        jidAndJournalMap.put(30905, "Am J Hematol");
        jidAndJournalMap.put(31251, "J Exp Clin Cancer Res");
        jidAndJournalMap.put(31652, "Brain Stimul");
        jidAndJournalMap.put(32488, "Neurosci Biobehav Rev");
        jidAndJournalMap.put(32564, "Am J Public Health");
        jidAndJournalMap.put(33085, "JHEP Rep");
        jidAndJournalMap.put(33153, "Int J Behav Nutr Phys Act");
        jidAndJournalMap.put(33157, "J Int AIDS Soc");
        jidAndJournalMap.put(33702, "Clin Microbiol Rev");
        jidAndJournalMap.put(33779, "Oncogene");
        jidAndJournalMap.put(34896, "J Bone Joint Surg Am");
        jidAndJournalMap.put(34916, "Lancet Child Adolesc Health");
        jidAndJournalMap.put(35359, "J Thorac Cardiovasc Surg");
        jidAndJournalMap.put(35375, "Pharmacol Ther");
        jidAndJournalMap.put(35529, "J Headache Pain");
        jidAndJournalMap.put(35554, "Am J Surg Pathol");
        jidAndJournalMap.put(35562, "JACC Clin Electrophysiol");
        jidAndJournalMap.put(35777, "EuroIntervention");
        jidAndJournalMap.put(35793, "Am J Sports Med");
        jidAndJournalMap.put(36188, "Blood Adv");
        jidAndJournalMap.put(36223, "Clin Cancer Res");
        jidAndJournalMap.put(36613, "Intensive Care Med");
        jidAndJournalMap.put(36706, "Lancet HIV");
        jidAndJournalMap.put(37016, "Bone Joint J");
        jidAndJournalMap.put(37356, "Autoimmun Rev");
        jidAndJournalMap.put(37960, "J Control Release");
        jidAndJournalMap.put(38012, "Circ Cardiovasc Interv");
        jidAndJournalMap.put(38262, "Acta Pharm Sin B");
        jidAndJournalMap.put(38502, "Lancet Gastroenterol Hepatol");
        jidAndJournalMap.put(38767, "J Allergy Clin Immunol Pract");
        jidAndJournalMap.put(39313, "Spine J");
        jidAndJournalMap.put(39487, "Biomaterials");
        jidAndJournalMap.put(40167, "Mol Ther");
        jidAndJournalMap.put(40420, "Nat Rev Drug Discov");
        jidAndJournalMap.put(40797, "Circ Res");
        jidAndJournalMap.put(41855, "Dent Mater");
        jidAndJournalMap.put(42182, "Prog Retin Eye Res");
        jidAndJournalMap.put(42476, "BMJ");
        jidAndJournalMap.put(42582, "Ann Oncol");
        jidAndJournalMap.put(43178, "Schizophr Bull");
        jidAndJournalMap.put(43186, "Nat Metab");
        jidAndJournalMap.put(43366, "Nat Rev Dis Primers");
        jidAndJournalMap.put(43756, "Ann Intern Med");
        jidAndJournalMap.put(43893, "Sci Immunol");
        jidAndJournalMap.put(43947, "Hum Reprod");
        jidAndJournalMap.put(44082, "J Clin Oncol");
        jidAndJournalMap.put(44350, "Arthroscopy");
        jidAndJournalMap.put(44771, "Gastroenterology");
        jidAndJournalMap.put(45145, "J Hematol Oncol");
        jidAndJournalMap.put(46492, "Cell Stem Cell");
        jidAndJournalMap.put(46510, "BJOG");
        jidAndJournalMap.put(46558, "Fertil Steril");
        jidAndJournalMap.put(46593, "JAMA Dermatol");
        jidAndJournalMap.put(46673, "Am J Obstet Gynecol");
        jidAndJournalMap.put(46824, "J Bone Miner Res");
        jidAndJournalMap.put(46850, "Hum Reprod Update");
        jidAndJournalMap.put(47162, "Nat Rev Rheumatol");
        jidAndJournalMap.put(47618, "J Hepatol");
        jidAndJournalMap.put(48786, "Arterioscler Thromb Vasc Biol");
        jidAndJournalMap.put(49621, "Nat Rev Cardiol");
        jidAndJournalMap.put(49940, "Aging Cell");
        jidAndJournalMap.put(50188, "Implement Sci");
        jidAndJournalMap.put(50629, "J Natl Cancer Inst");
        jidAndJournalMap.put(50964, "MMWR Morb Mortal Wkly Rep");
        jidAndJournalMap.put(51067, "Haematologica");
        jidAndJournalMap.put(51313, "Lancet");
        jidAndJournalMap.put(51780, "Gut");
        jidAndJournalMap.put(51786, "Eur Respir J");
        jidAndJournalMap.put(51820, "Adv Drug Deliv Rev");
        jidAndJournalMap.put(52082, "Eur Respir Rev");
        jidAndJournalMap.put(52503, "PLoS Med");
        jidAndJournalMap.put(52515, "NPJ Digit Med");
        jidAndJournalMap.put(52597, "Radiother Oncol");
        jidAndJournalMap.put(52892, "Int J Radiat Oncol Biol Phys");
        jidAndJournalMap.put(53584, "J Extracell Vesicles");
        jidAndJournalMap.put(54070, "Ocul Surf");
        jidAndJournalMap.put(54206, "Cell Host Microbe");
        jidAndJournalMap.put(54858, "Neuro Oncol");
        jidAndJournalMap.put(54903, "Anesthesiology");
        jidAndJournalMap.put(55161, "Endoscopy");
        jidAndJournalMap.put(55221, "Alzheimers Dement");
        jidAndJournalMap.put(55298, "Brain");
        jidAndJournalMap.put(55987, "J Clin Periodontol");
        jidAndJournalMap.put(56368, "Eur Heart J");
        jidAndJournalMap.put(56429, "J Am Soc Nephrol");
        jidAndJournalMap.put(56805, "Chest");
        jidAndJournalMap.put(57246, "Kidney Int");
        jidAndJournalMap.put(57338, "Lancet Glob Health");
        jidAndJournalMap.put(57351, "Semin Cancer Biol");
        jidAndJournalMap.put(58128, "Ann Rheum Dis");
        jidAndJournalMap.put(58221, "Signal Transduct Target Ther");
        jidAndJournalMap.put(58226, "Int Endod J");
        jidAndJournalMap.put(58545, "Cancer Cell");
        jidAndJournalMap.put(59571, "J Nucl Med");
        jidAndJournalMap.put(59930, "Acta Neuropathol");
        jidAndJournalMap.put(60067, "Diabetologia");
        jidAndJournalMap.put(60199, "Am J Psychiatry");
        jidAndJournalMap.put(60601, "J Thorac Oncol");
        jidAndJournalMap.put(61370, "Allergy");
        jidAndJournalMap.put(61631, "Biol Psychiatry");
        jidAndJournalMap.put(61892, "Bioact Mater");
        jidAndJournalMap.put(61955, "EMBO Mol Med");
        jidAndJournalMap.put(62410, "Ann Intensive Care");
        jidAndJournalMap.put(62510, "Neurology");
        jidAndJournalMap.put(62720, "J Exp Med");
        jidAndJournalMap.put(62736, "IEEE Trans Med Imaging");
        jidAndJournalMap.put(63147, "Theranostics");
        jidAndJournalMap.put(63355, "J Orthop Translat");
        jidAndJournalMap.put(64260, "Br J Anaesth");
        jidAndJournalMap.put(64864, "J Am Coll Cardiol");
        jidAndJournalMap.put(65973, "Diabetes");
        jidAndJournalMap.put(66175, "Health Aff (Millwood)");
        jidAndJournalMap.put(66431, "Neurol Neuroimmunol Neuroinflamm");
        jidAndJournalMap.put(66759, "Lancet Public Health");
        jidAndJournalMap.put(66817, "Cancer Discov");
        jidAndJournalMap.put(66844, "Clin Gastroenterol Hepatol");
        jidAndJournalMap.put(67246, "Circulation");
        jidAndJournalMap.put(67288, "Nat Rev Urol");
        jidAndJournalMap.put(67411, "Nat Rev Clin Oncol");
        jidAndJournalMap.put(67653, "Endocr Rev");
        jidAndJournalMap.put(67976, "Immunity");
        jidAndJournalMap.put(68050, "JAMA Pediatr");
        jidAndJournalMap.put(68622, "JAMA Otolaryngol Head Neck Surg");
        jidAndJournalMap.put(68636, "Lancet Neurol");
        jidAndJournalMap.put(69337, "J Allergy Clin Immunol");
        jidAndJournalMap.put(70150, "Med Res Rev");
        jidAndJournalMap.put(70202, "Genet Med");
        jidAndJournalMap.put(70246, "JACC Heart Fail");
        jidAndJournalMap.put(70757, "Nat Rev Cancer");
        jidAndJournalMap.put(71499, "PLoS Pathog");
        jidAndJournalMap.put(71530, "Nat Rev Neurol");
        jidAndJournalMap.put(71940, "Metabolism");
        jidAndJournalMap.put(72386, "Ageing Res Rev");
        jidAndJournalMap.put(72409, "J Immunother Cancer");
        jidAndJournalMap.put(73077, "Mov Disord");
        jidAndJournalMap.put(73130, "Circ Arrhythm Electrophysiol");
        jidAndJournalMap.put(73383, "Sports Med");
        jidAndJournalMap.put(74121, "BMC Med");
        jidAndJournalMap.put(74159, "Pain");
        jidAndJournalMap.put(75572, "JAMA Oncol");
        jidAndJournalMap.put(75699, "Biochim Biophys Acta Rev Cancer");
        jidAndJournalMap.put(75905, "J Clin Invest");
        jidAndJournalMap.put(76798, "Sci Transl Med");
        jidAndJournalMap.put(77080, "Acta Pharmacol Sin");
        jidAndJournalMap.put(77136, "Br J Sports Med");
        jidAndJournalMap.put(77287, "JAMA Psychiatry");
        jidAndJournalMap.put(77658, "J Am Acad Dermatol");
        jidAndJournalMap.put(77690, "J Child Psychol Psychiatry");
        jidAndJournalMap.put(77848, "Nat Med");
        jidAndJournalMap.put(77872, "Cardiovasc Res");
        jidAndJournalMap.put(78462, "Crit Care Med");
        jidAndJournalMap.put(78600, "Diabetes Care");
        jidAndJournalMap.put(78719, "JCI Insight");
        jidAndJournalMap.put(78789, "JAMA Surg");
        jidAndJournalMap.put(79704, "Leukemia");
        jidAndJournalMap.put(80476, "Lancet Oncol");
        jidAndJournalMap.put(81284, "J Neuroinflammation");
        jidAndJournalMap.put(81809, "JAMA Ophthalmol");
        jidAndJournalMap.put(82113, "Cancer Immunol Res");
        jidAndJournalMap.put(82191, "Trends Endocrinol Metab");
        jidAndJournalMap.put(82405, "Radiol Med");
        jidAndJournalMap.put(82631, "Am J Respir Crit Care Med");
        jidAndJournalMap.put(82717, "Europace");
        return jidAndJournalMap;
    }


}
